package ru.vba.system;

import javax.annotation.PostConstruct;

import ru.vba.utils.MongoUtils;

import com.google.code.morphia.Datastore;

public abstract class AbstractBean<T extends Substance> implements SaveableBean<T>{

	private Datastore store;
	@PostConstruct
	public void init(){
		store = MongoUtils.getDatastore(getClazz());
	}
	public T getById(Long id) {
		return store.find(getClazz()).field("id").equal(id).get();
	}

	public void save(T substance) {
		if (substance.getId() == null){
			store.save(substance);
		}else{
			store.update(substance, store.createUpdateOperations(getClazz()));
		}
	}

	public void delete(T substance) {
		store.delete(substance);
	}
	protected abstract Class<T> getClazz();
}
